
var dblMainScene = {
    runThisScene:function(){

        this.ui = new cc.Scene()
       
        cc.BuilderReader.registerController("dblMain", {});
        this._initCallBack();

        var node  = cc.BuilderReader.load("ccb/dblMain.ccbi", this);
        this.animationManager = node.animationManager
        this.ui.addChild(node)

        this.refreshMain()

        // createNameEditBox(env.deskNumInputBox, '输入房间人数')
        // createNameEditBox(env.bottomBetInputBox, '输入底注')
        for(var i=1;i<3;i++)
        {
            var slider = new ccui.Slider()
            slider.setTouchEnabled(true)
            slider.loadBarTexture("loadBar.png")
            slider.loadSlidBallTextures("loadSlidBall.png","loadSlidBall.png","")
            slider.loadProgressBarTexture("loadProgressBar.png")
            slider.x = this['sliderNode'+i].width / 2.0
            slider.y = this['sliderNode'+i].height / 2.0 - 20

            function a() 
            {
                var a = arguments[0]
                var t= arguments[1]
                slider.addEventListener(function(sender, type){t.sliderEvent(a, sender, type)},t)
            }
            a(i,this)
            this['sliderNode'+i].addChild(slider)
        }

        this.deskNumData = 2
        this.bottomBetData = 100
        this.createRoomBoard.setVisible(false)


        if (!dblRoomLayer.hasInit)
            dblRoomLayer.initLayer()
        dblRoomLayer.ccbNode.setVisible(false)
        this.ui.addChild(dblRoomLayer.ccbNode)
       
        cc.director.runScene(this.ui)

    },
    selectedItemEvent:function(sender, type){

        if(type==ccui.ListView.EVENT_SELECTED_ITEM)
        {
        }
        else if(type==ccui.ListView.ON_SELECTED_ITEM_END)
        {
            var listViewEx = sender
            cc.log("select child index = " + listViewEx.getCurSelectedIndex())
            var roomId = listViewEx.rooms[listViewEx.getCurSelectedIndex()].id
            ioSocket.req('joinRoom',{id:roomId}) 
        }
    },
    appendRoomsList:function(roomsList)
    {   
        if(!this.listView )
        {
            this.listView = new ccui.ListView()
            var listView = this.listView 
            listView.setDirection(ccui.ScrollView.DIR_VERTICAL)
            listView.setTouchEnabled(true)
            listView.setBounceEnabled(true)
            //listView.setBackGroundImage("MainScene/inputbox.png")
            listView.setBackGroundImageScale9Enabled(true)

            listView.setContentSize(this.roomListNode.getContentSize().width,this.roomListNode.getContentSize().height-10)
            listView.x = 0
            listView.y = 0
            listView.addEventListener(this.selectedItemEvent, this)
            this.roomListNode.addChild(listView)

            listView.rooms = []
        }

        function getItem(listView)
        {
            var default_button = new ccui.Button()
            default_button.setName("room")
            default_button.setTouchEnabled(true)
            default_button.setScale9Enabled(true)
            default_button.loadTextures("MainScene/inputbox.png", "MainScene/inputbox.png", "")
            default_button.setCapInsets(cc.rect(0, 0, 0, 0));
            default_button.setContentSize(listView.width-10, 80)
            var default_item = new ccui.Layout();
            default_item.setTouchEnabled(true)
            default_item.setContentSize(default_button.getContentSize())
            default_item.width = listView.width
            default_button.x = default_item.width / 2
            default_button.y = default_item.height / 2 - 10
            default_item.addChild(default_button)

            return default_item
        }

        var listView = this.listView 
        for(var i in roomsList)
        {   
            var length = listView.rooms.length
            listView.rooms[length] = roomsList[i]
            //listView.pushBackDefaultItem()
            //listView.insertDefaultItem(0)
            listView.pushBackCustomItem(getItem(listView)) //如果要加一个和default_button不一样的奇形怪状 可以用这个

            var item = listView.getItem(listView.getItems().length-1)
            var button = item.getChildByName("room")
            //var index = listView.getIndex(item)
            button.setTitleText('ID:' + roomsList[i].id + '              ' +
                '桌数:' + roomsList[i].deskNum + '              ' +
                '底注:' + roomsList[i].bottomBet)
            button.setTitleColor(cc.color(242, 169, 123, 255) )
            button.setTitleFontSize(24)
            //listView.removeLastItem();
            //listView.removeItem(items_count - 1);
        }

    },
    sliderEvent:function (sliderIndex, sender, type) {
        if(type==ccui.Slider.EVENT_PERCENT_CHANGED)
        {   
            var slider = sender
            var percent = slider.getPercent()
            if(sliderIndex==1)
            {
                var num = Math.ceil(percent/50) + 1
                if(num==1) num = 2
                this['sliderTTF'+sliderIndex].setString("桌数:" + num)
                this.deskNumData = num
            }
            else
            {
                var maxNum = Math.floor(USERDATA.gold/5000) 
                var num = Math.ceil(maxNum * percent / 100)
                if(num==0) num = 1
                num = num * 100
                this['sliderTTF'+sliderIndex].setString("底注:" + num)
                this.bottomBetData = num
            }
        }
    },
    refreshMain:function()
    {   
        this.nameTxt.setString(USERDATA.name)
        this.goldTxt.setString('游戏币:' + USERDATA.gold)
    },
    _initCallBack:function()
    {
        this.joinRoom = function()
        {
            cc.log("joinRoom")
            ioSocket.req('quickJoin') 
        }
        this.createRoom = function()
        {
            this.createRoomBoard.setVisible(true)
        }
        this.sureCreate = function()
        {
            cc.log("create")
            ioSocket.req('createRoom',{deskNum:this.deskNumData, bottomBet:this.bottomBetData})
            this.createRoomBoard.setVisible(false)
        }
        this.closeCreate = function()
        {
           this.createRoomBoard.setVisible(false)
        }
    },

}

